package com.awan.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
//import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.awan.utils.Constanta;
import com.beem.project.beem.R;

public class DataAdapter extends BaseAdapter{

	Activity mActivity;
	Context mContext;
	private String [] id = {"Contact", "Request", "Friends Update", "     Your Connections", "My Chats", "Messages", 
			"Chat with Artist", "Visitors", "Notification", "Join Room", "Check Point"};
	public static String _requestData;
    private LayoutInflater mInflater;
    
    public DataAdapter(Activity act, Context c, String requestData){
    	
    	   mActivity=act;
           mContext=c;
           mInflater = LayoutInflater.from(c);
           
           if(requestData != null)
        	   _requestData = requestData;
           
    }
    public int getCount(){
           return id.length;
    }
    public Object getItem(int position){
           return position;
    }
    public long getItemId(int position){
           return position;
    }
    public View getView(int position, View convertView, ViewGroup parent){
    	boolean isList=true;
    	
           ViewHolder holder=null;
           if(convertView==null)
           {
        	   holder = new ViewHolder();
        	   if(id[position].equals("     Your Connections") || id[position].equals("Other Chat")){
        		   convertView = mInflater.inflate(R.layout.menu_leftdevider, parent,false);
        		   isList=false;
        	   }else{
        		   convertView = mInflater.inflate(R.layout.menu_leftlist, parent,false); 
        		   holder.txtComplement=(TextView)convertView.findViewById(R.id.txtComplement);
                   holder.txtComplement.setPadding(0, 0, 10, 0);
                   
                   holder.iconLeft=(ImageView)convertView.findViewById(R.id.imageView1);
        		   
        	   }   
                  
                  holder.txtId=(TextView)convertView.findViewById(R.id.txtId);
                  holder.txtId.setPadding(10, 10,10 , 10);                  
                  
           }else{

        	   holder=null;
        	   holder = new ViewHolder();
         	   if(id[position].equals("     Your Connections") || id[position].equals("Other Chat")){
         		   convertView = mInflater.inflate(R.layout.menu_leftdevider, parent,false);
         		   isList=false;
         	   }else{
         		   convertView = mInflater.inflate(R.layout.menu_leftlist, parent,false); 
         		   holder.txtComplement=(TextView)convertView.findViewById(R.id.txtComplement);
                    holder.txtComplement.setPadding(0, 0, 10, 0);
                    
                    holder.iconLeft=(ImageView)convertView.findViewById(R.id.imageView1);
         		   
         	   }   
                  
                  holder.txtId=(TextView)convertView.findViewById(R.id.txtId);
                  holder.txtId.setPadding(10, 10,10 , 10);
                  
           }
       
         holder.txtId.setText(id[position]);
         if(isList){
        	 switch(position){
        	 case 0:
        		 holder.iconLeft.setImageResource(R.drawable.left_contact);
        		 break;
        	 case 1:
        		 //Log.e("DataAdapter", "left menu request");
        		 holder.txtComplement.setText(getRequestCount());
        		 holder.iconLeft.setImageResource(R.drawable.left_request);
        		 break;
        	 case 2:
        		 holder.iconLeft.setImageResource(R.drawable.left_friendupdate);
        		 break;
             case 4:
            	 holder.txtComplement.setText(String.valueOf(Constanta.currentChatCount));
            	 holder.iconLeft.setImageResource(R.drawable.left_mychat);
            	 break;
             case 5:
            	 holder.txtComplement.setText("2");
            	 holder.iconLeft.setImageResource(R.drawable.left_message);
            	 break;	 
             case 6:
            	 holder.iconLeft.setImageResource(R.drawable.left_chatartist);
            	 break;
             case 7:
            	 holder.txtComplement.setText("12");
            	 holder.iconLeft.setImageResource(R.drawable.left_visitor);
            	 break;
             case 8:
            	 holder.txtComplement.setText("5");
            	 holder.iconLeft.setImageResource(R.drawable.left_notification);
            	 break;
             case 9:
            	 holder.iconLeft.setImageResource(R.drawable.left_joinroom);
            	 break;
             case 10:
            	 holder.iconLeft.setImageResource(R.drawable.left_checkpoint);
            	 holder.txtComplement.setText("135");
            	 break;
             }
         }
         return convertView;
    }
    static class ViewHolder{       
    	ImageView iconLeft;
    	TextView txtId;        
    	TextView txtComplement;               
    }
    
    private String getRequestCount(){
    	
    	String result = "0";
    	
    	 if(_requestData != null){
    		 
    		 //Log.e("DataAdapter", "this._requestData != null");
    		 
    		 try {
    			 
				JSONObject jsonResponse = new JSONObject(_requestData);
				String status = jsonResponse.getString("STATUS");
				
				//Log.e("DataAdapter", status);
				
				if(status.trim().equalsIgnoreCase("SUCCESS")){
					
					JSONArray data = new JSONArray(jsonResponse.getString("DATA"));
					//Log.e("DataAdapter", "count:"+String.valueOf(data.length()));
					result = String.valueOf(data.length());
					
				}
				
			} catch (JSONException e) {
				
				// TODO Auto-generated catch block
				Log.e("DataAdapter", e.getMessage());
				e.printStackTrace();
				
			}
    		 
    	 }
    	// Log.e("DataAdapter", "result:"+result);
    	return result;
    }
}
